package rcp.iphex2.ui.knownsubstances;

import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;

import rcp.iphex2.model.AreaAnnotation;
import rcp.iphex2.model.ModelProvider;
import rcp.iphex2.model.Project;

public class KnownSubstancesView extends ViewPart {

	private TableViewer viewer;
	private Project activeProject;
	private KnownSubstancesFilter filter;

	public KnownSubstancesView() {
		// TODO Auto-generated constructor stub
	}

	// public void setProject(Project project) {
	// activeProject = project;
	// }

	@Override
	public void createPartControl(Composite parent) {

		// read known substances here
		// Project project = ModelProvider.INSTANCE.getProject("Default");
		// setProject(project);

		createTableViewer(parent);
		addMenu();
	}

	private void createTableViewer(Composite parent) {
		GridLayout layout = new GridLayout(2, false);
		parent.setLayout(layout);
		Label searchLabel = new Label(parent, SWT.NONE);
		searchLabel.setText("Search: ");
		final Text searchText = new Text(parent, SWT.BORDER | SWT.SEARCH);
		searchText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
		createViewer(parent);
		// Set the sorter for the table
		// comparator = new MyViewerComparator();
		// viewer.setComparator(comparator);

		// New to support the search
		searchText.addKeyListener(new KeyAdapter() {
			public void keyReleased(KeyEvent k) {
				filter.setSearchText(searchText.getText());
				viewer.refresh();
			}

			public void keyPressed(KeyEvent k) {
				if (k.keyCode == SWT.ARROW_DOWN) {
					int selectionIndex = viewer.getTable().getSelectionIndex();
					selectionIndex++;
					viewer.getTable().setSelection(selectionIndex);
				}
				if (k.keyCode == SWT.ARROW_UP) {
					int selectionIndex = viewer.getTable().getSelectionIndex();
					selectionIndex--;
					if (selectionIndex < 0)
						selectionIndex = 0;
					viewer.getTable().setSelection(selectionIndex);
				}
			}

		});
		filter = new KnownSubstancesFilter();
		viewer.addFilter(filter);
	}

	private void createViewer(Composite parent) {
		viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
		createColumns(parent, viewer);
		final Table table = viewer.getTable();
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		viewer.setContentProvider(new ArrayContentProvider());
		// Get the content for the viewer, setInput will call getElements in the
		// contentProvider
		viewer.setInput(ModelProvider.INSTANCE.getAreaAnnotations());
		// Make the selection available to other views
		getSite().setSelectionProvider(viewer);

		// Layout the viewer
		GridData gridData = new GridData();
		gridData.verticalAlignment = GridData.FILL;
		gridData.horizontalSpan = 2;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalAlignment = GridData.FILL;
		viewer.getControl().setLayoutData(gridData);
	}

	// This will create the columns for the table
	private void createColumns(final Composite parent, final TableViewer viewer) {
		String[] titles = { "Name", "RT Position", "1/k0 Position" };
		int[] bounds = { 100, 100, 100 };

		// First column is for the first name
		TableViewerColumn col = createTableViewerColumn(titles[0], bounds[0], 0);
		col.setLabelProvider(new ColumnLabelProvider() {
			@Override
			public String getText(Object element) {
				AreaAnnotation areaAnnotation = (AreaAnnotation) element;
				return areaAnnotation.getName();
			}
		});
		col = createTableViewerColumn(titles[1], bounds[1], 1);
		col.setLabelProvider(new ColumnLabelProvider() {
			@Override
			public String getText(Object element) {
				AreaAnnotation areaAnnotation = (AreaAnnotation) element;
				return areaAnnotation.getRt() + "";
			}
		});
		col = createTableViewerColumn(titles[2], bounds[2], 2);
		col.setLabelProvider(new ColumnLabelProvider() {
			@Override
			public String getText(Object element) {
				AreaAnnotation areaAnnotation = (AreaAnnotation) element;
				return areaAnnotation.getDt() + "";
			}
		});

	}

	private TableViewerColumn createTableViewerColumn(String title, int bound, final int colNumber) {
		final TableViewerColumn viewerColumn = new TableViewerColumn(viewer, SWT.NONE);
		final TableColumn column = viewerColumn.getColumn();
		column.setText(title);
		column.setWidth(bound);
		column.setResizable(true);
		column.setMoveable(true);
		return viewerColumn;
	}

	private void addMenu() {
		MenuManager menuManager = new MenuManager();
		Menu menu = menuManager.createContextMenu(viewer.getControl());
		viewer.getControl().setMenu(menu);
		getSite().registerContextMenu(menuManager, viewer);
	}

	@Override
	public void setFocus() {
		viewer.getControl().setFocus();
	}

}
